﻿using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using System.Collections.Generic;

using Styx;
using Styx.Plugins;
using Styx.Common;
using Styx.CommonBot;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace rndbuddy_csourcer
{
    static class crowdsourcer
    {
        public static NameValueCollection nodepool = new NameValueCollection();
        public static int total_found =0;

        public static void get_nodes()
        {     
            var NodeList = (from o in ObjectManager.GetObjectsOfType<WoWGameObject>(false, false)
                            where (o.IsHerb || o.IsMineral)
                            orderby o.DistanceSqr ascending
                            select o).ToList();

            foreach (WoWGameObject node in NodeList)
            {
                string isIndoor = "0";
                if (node.IsIndoors)
                    isIndoor ="1";

                string data = StyxWoW.Me.MapId + ";" +
                              StyxWoW.Me.ZoneId + ";" +
                              StyxWoW.Me.ZoneText + ";" +
                              node.Name + ";" +
                              node.DisplayId.ToString() + ";" +
                              node.X.ToString() + ";" +
                              node.Y.ToString() + ";" +
                              node.Z.ToString() + ";" +
                              isIndoor + ";" +
                              StyxWoW.Me.SubZoneText;
                        
                string id = Crypto.GetHashString(data);
                if (!is_stored(id))
                {
                    nodepool.Add(id, data);
                    total_found++;
                }
            }
        }

        public static string merge_and_report(bool reset_collection)
        {
            var array = (from key in nodepool.AllKeys
                         from value in nodepool.GetValues(key)
                         select value).ToArray();

            //empty namevaluecollection
            if (reset_collection)
                nodepool = new NameValueCollection();

            return (string.Join(";;;;", array));
        }

        public static bool is_stored(string id)
        {
            string value = nodepool[id];
            if (value == null)
                return false;
            return true;
        }

        public static int amount_of_nodes()
        {
            return nodepool.Count;
        }
    }
}
